home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig14_08.jar / Ch14 / Fig14_08 / Fig14_08.paul < prev    next >
Text File  |  1997-08-26  |  2KB  |  92 lines

  1. // fig14_8.cpp
  2. // Credit inquiry program
  3. #include <iostream.h>
  4. #include <fstream.h>
  5. #include <iomanip.h>
  6. #include <stdlib.h> 
  7.  
  8. void outputLine( int, char*, double );
  9.  
  10. main()
  11. {
  12.    ifstream inClientFile( "clients.dat", ios::in );
  13.  
  14.    if ( !inClientFile ) {
  15.       cerr << "File could not be opened" << endl;
  16.       exit( 1 );
  17.    }
  18.  
  19.    cout << "Enter request\n"
  20.         << " 1 - List accounts with zero balances\n"
  21.         << " 2 - List accounts with credit balances\n"
  22.         << " 3 - List accounts with debit balances\n" 
  23.         << " 4 - End of run\n? ";
  24.  
  25.    int request;
  26.    cin >> request;
  27.  
  28.    while ( request != 4 ) {
  29.       int account;
  30.       char name[ 10 ];
  31.       double balance;
  32.  
  33.       inClientFile >> account >> name >> balance;
  34.  
  35.       switch (request) {
  36.          case 1:
  37.             cout << endl << "Accounts with zero balances:" << endl;
  38.  
  39.             while (!inClientFile.eof()) {
  40.                if (balance == 0)
  41.                   outputLine(account, name, balance);
  42.  
  43.                inClientFile >> account >> name >> balance;
  44.             }
  45.  
  46.             break;
  47.          case 2:
  48.             cout << endl << "Accounts with credit balances:" 
  49.                  << endl;
  50.  
  51.             while (!inClientFile.eof()) {
  52.                if (balance < 0)
  53.                   outputLine(account, name, balance);
  54.  
  55.                inClientFile >> account >> name >> balance;
  56.             }
  57.  
  58.             break;
  59.          case 3:
  60.             cout << endl << "Accounts with debit balances:" 
  61.                  << endl;
  62.  
  63.             while (!inClientFile.eof()) {
  64.                if (balance > 0)
  65.                   outputLine(account, name, balance);
  66.  
  67.                inClientFile >> account >> name >> balance;
  68.             }
  69.  
  70.             break;
  71.       }
  72.  
  73.       inClientFile.clear();  // reset eof for next input
  74.       inClientFile.seekg(0); // position to beginning of file
  75.       cout << endl << "? ";
  76.       cin >> request;
  77.    }
  78.  
  79.    cout << "End of run." << endl;
  80.  
  81.    return 0;
  82. }
  83.  
  84. void outputLine(int acct, char *name, double bal)
  85. {
  86.    cout << setiosflags(ios::left) << setw(10) << acct
  87.         << setw(13) << name << setw(7) << setprecision(2)
  88.         << setiosflags(ios::showpoint | ios::right)
  89.         << bal << endl;
  90. }
  91.  
  92.